import React, { Component } from 'react'
import PubSub from 'pubsub-js'
import './Header.css'
export default class Header extends Component {
  handle = (e) => {
    // 直接获取用户输入的value
    const value = e.target.value.trim()
    //如果不是回车,或输入内容为空则直接return
    if (e.keyCode !== 13 || !value) return

    // 将任务名传递给App
    PubSub.publish('add', value)

    // 清空文本框
    e.target.value = ''
  }
  render() {
    return (
      <div className="todo-header">
        <input type="text" onKeyDown={this.handle} />
      </div>
    )
  }
}
